import { observable, action, toJS } from 'mobx';
import _ from "lodash";
import { message, Modal } from 'antd';
import window from '../../util';
import {createContract,getCompanys} from "../../apis/dev";
class ElectricSignStore {
    @observable loading = true;
    @observable params: any;
    @observable signType: string = '1';
    @observable companyname: string = '';
    @observable companyvisible: boolean = false;
    @observable selectedFile: string = "";
    @observable selectedFileData: any = {};
    @observable refresh: number = 0;
    @observable companys: any = [];

    @action
    init = () => {
        getCompanys({}).then((result: any) => {
            if (result.status == "1"&&result.data&&result.data.length>0) {
                this.companys=result.data;
                this.companys.map(com=>{
                    if(com.selected){
                        this.companyname=com.showname;
                    }
                })
                if(this.companyname==''){
                    this.companyname=this.companys[0].showname;
                }
            }
        })
    }

    getOptions = () =>{
        let options = new Array();
        this.companys.map(com=>{
            options.push({key:com.key,showname:com.showname,selected:com.selected});
        })
        return options;
    }



    @action
    updateParams = (params: any) => {
        let temp = !_.isEqual(toJS(this.params), params);
        this.params = params;
        if (temp) {
            this.refresh += 1;
        }
    }

    @action
    changeSignType = (value) => {
        this.signType = value;
        if(value=='2'){
            this.companyvisible=true;
        }else{
            this.companyvisible=false;
        }
    }

    @action
    getShowListDatas = (__: any, newData: any[]) => {
        if (newData && newData.length == 1) {
            this.selectedFile = _.get(newData, '[0].name');
            this.selectedFileData = { filename: _.get(newData, '[0].name'), fileid: "-999", size: _.get(newData, '[0].size') };
        } else {
            this.selectedFile = "";
        }
    }

    @action
    deleteFile = () => {
        const that = this;
        Modal.confirm({
            title: '系统提示',
            content: '你确认删除该待上传文件吗？',
            onOk() {
                that.selectedFile = "";
                that.selectedFileData = {};
                that.refresh += 1;
            },
            onCancel() {
            },
        });
    }

    @action
    toSign = () => {
        if(this.signType=='2'&&this.companyname==''){
            message.error("企业名称不能为空!");
            return;
        }
        (window as any).startUploadAll();
    }

    @action
    uploadChange = (ids, list) => {
        if (ids && ids.length == 1) {
            let excelid = ids[0];
            let filename=list[0].filename;
		  	filename=filename.replace(/\..*/g,'');
            const params = {
                fieldid: excelid,
                filename:filename,
                signtype:this.signType,
                companyname:this.companyname
            }
            //附件上传成功去签章
            createContract(params).then((data: any) => {
                if (data.status == "1"&&data.url) {
                    window.openFullWindowHaveBar(data.url);
                } else {
                    Modal.error({
                        title: "创建失败",
                        width: 500,
                        content: <div style={{ maxHeight: 600, overflowY: 'auto' }}><div dangerouslySetInnerHTML={{ __html: "创建签章文件发生错误" }}></div></div>,
                        onOk() { },
                    });
                }
            })
        } else {
            message.error("文件上传失败！");
        }
    }

}
export default ElectricSignStore;